package com.longge;

import java.util.HashMap;
import java.util.Map;

public class CanConstructII {

    public boolean canConstruct(String ransomNote, String magazine) {

        char[] ransomNoteChars = ransomNote.toCharArray();
        char[] magazineChars = magazine.toCharArray();

        HashMap<Character, Integer> mapR = new HashMap<>();
        HashMap<Character, Integer> mapM = new HashMap<>();

        for (char ransomNoteChar : ransomNoteChars) {
            mapR.put(ransomNoteChar, mapR.getOrDefault(ransomNoteChar, 0) + 1);
        }

        for (char mChar : magazineChars) {
            mapM.put(mChar, mapM.getOrDefault(mChar, 0) + 1);
        }
        for (Map.Entry<Character, Integer> entry : mapR.entrySet()) {
            Character rChar = entry.getKey();
            if (mapM.getOrDefault(rChar,0)<entry.getValue()){
                return false;
            }
        }
        return true;

    }

}
